package pe.com.bbva.modpre.utileria;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Map;

import com.vaadin.data.Container;
import com.vaadin.ui.TreeTable;

@SuppressWarnings("serial")
public class ArbolTablaEvento extends TreeTable{

	protected static Method		LISTENER_METHOD_COLUMN_ORDER_CHANGED;
	
	protected static Method		LISTENER_METHOD_COLUMN_SHOWN_CHANGED;
	
	protected static Method		LISTENER_METHOD_COLUMN_SORT_CHANGED;
	
	static {
		try {
			LISTENER_METHOD_COLUMN_SHOWN_CHANGED = ColumnShownChangedListener.class.getDeclaredMethod( "columnShownChanged", new Class[] { ColumnShownChangedEvent.class } );
		} catch ( SecurityException e ) {
			throw new RuntimeException( e );
		} catch ( NoSuchMethodException e ) {
			throw new RuntimeException( e );
		}
		try {
			LISTENER_METHOD_COLUMN_ORDER_CHANGED = ColumnOrderChangedListener.class.getDeclaredMethod( "columnOrderChanged", new Class[] { ColumnOrderChangedEvent.class } );
		} catch ( SecurityException e ) {
			throw new RuntimeException( e );
		} catch ( NoSuchMethodException e ) {
			throw new RuntimeException( e );
		}
		try {
			LISTENER_METHOD_COLUMN_SORT_CHANGED = ColumnSortChangedListener.class.getDeclaredMethod( "columnSortChanged", new Class[] { ColumnSortChangedEvent.class } );
		} catch ( SecurityException e ) {
			throw new RuntimeException( e );
		} catch ( NoSuchMethodException e ) {
			throw new RuntimeException( e );
		}
	}
	
	public ArbolTablaEvento() {
		super();
	}
	
	public ArbolTablaEvento( String caption ) {
		super( caption );
	}
	
	public ArbolTablaEvento( String caption, Container dataSource ) {
		super( caption, dataSource );
	}
	
	public void addListener( ColumnOrderChangedListener listener ) {
		addListener( ArbolTablaEvento.ColumnOrderChangedEvent.class, listener, LISTENER_METHOD_COLUMN_ORDER_CHANGED );
	}
	
	public void addListener( ColumnShownChangedListener listener ) {
		addListener( ArbolTablaEvento.ColumnShownChangedEvent.class, listener, LISTENER_METHOD_COLUMN_SHOWN_CHANGED );
	}
	
	public void addListener( ColumnSortChangedListener listener ) {
		addListener( ArbolTablaEvento.ColumnSortChangedEvent.class, listener, LISTENER_METHOD_COLUMN_SORT_CHANGED );
	}
	
	@Override
	public void changeVariables( Object source, Map<String, Object> variables ) {
		super.changeVariables( source, variables );
		
		if ( variables.containsKey( "collapsedcolumns" ) ) {
			fireColumnShowChanged();
		}
		
		if ( variables.containsKey( "columnorder" ) ) {
			fireColumnOrderChanged();
		}
		
		if ( variables.containsKey( "sortcolumn" ) || variables.containsKey( "sortascending" ) ) {
			fireColumnSortChanged();
		}
		
	}
	
	protected void fireColumnOrderChanged() {
		fireEvent( new ColumnOrderChangedEvent( this ) );
	}
	
	protected void fireColumnShowChanged() {
		fireEvent( new ColumnShownChangedEvent( this ) );
	}
	
	protected void fireColumnSortChanged() {
		fireEvent( new ColumnSortChangedEvent( this ) );
	}
	
	public static final class ColumnOrderChangedEvent extends Event {
		
		public ColumnOrderChangedEvent( ArbolTablaEvento source ) {
			super( source );
		}
	}
	
	public interface ColumnOrderChangedListener extends Serializable {
		
		void columnOrderChanged( ColumnOrderChangedEvent event );
	}
	
	public static final class ColumnShownChangedEvent extends Event {
		
		public ColumnShownChangedEvent( ArbolTablaEvento source ) {
			super( source );
		}
	}
	
	public interface ColumnShownChangedListener extends Serializable {
		
		void columnShownChanged( ColumnShownChangedEvent event );
	}
	
	public static final class ColumnSortChangedEvent extends Event {
		
		public ColumnSortChangedEvent( ArbolTablaEvento source ) {
			super( source );
		}
	}
	
	public interface ColumnSortChangedListener extends Serializable {
		
		void columnSortChanged( ColumnSortChangedEvent event );
	}
	
}
